
//
//  FaceViewController.m
//  新浪的弹出动画
//
//  Created by MyMac on 15/8/21.
//  Copyright (c) 2015年 MyMac. All rights reserved.
//

#import "FaceViewController.h"

@interface FaceViewController ()

@end

@implementation FaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];

    //人脸识别
    UIImage*image=[UIImage imageNamed:@"5882b2b7d0a20cf4e3b656de74094b36acaf9989.jpg"];
    

    
    UIImage*newImage=[self imageFormImage:image];
    UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 200, 200)];
    imageView.image=newImage;
    [self.view addSubview:imageView];
   
    CIImage*Cimage=[CIImage imageWithCGImage:image.CGImage];
    NSLog(@"%ld  %ld",newImage.imageOrientation,image.imageOrientation);
    CIContext*text=[CIContext contextWithOptions:nil];
    CIDetector*detector=[CIDetector detectorOfType:CIDetectorTypeFace context:text options:[NSDictionary dictionaryWithObjectsAndKeys:CIDetectorAccuracyHigh,CIDetectorAccuracy,nil]];
    NSArray*array=[detector featuresInImage:Cimage];
    for(NSInteger i=0;i<array.count;i++){
      CIFaceFeature*feature=array[i];
        CGFloat w=self.view.bounds.size.width/3.0;
        CGFloat h=w;
        CGFloat x=i%3*w;
        CGFloat y=i/3*h;
        UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];
        [self.view addSubview:imageView];
        imageView.backgroundColor=[UIColor redColor];
       NSLog(@"%@",NSStringFromCGRect(feature.bounds));
        
    //feature.bounds是脸部位置，，但截取时必须对照片做坐标转换
        CGImageRef ref=CGImageCreateWithImageInRect(newImage.CGImage, feature.bounds);
        UIImage*faceImage=[UIImage imageWithCGImage:ref];
        imageView.image=[self imageFormImage:faceImage];
        CGImageRelease(ref);
    
    
   }
    

 //  [self faceSelector];
 //   [self QRSelector];
}
-(void)faceSelector
{
    UIScrollView*scroll=[[UIScrollView alloc]initWithFrame:self.view.bounds];
    scroll.contentSize=CGSizeMake(1000, 1000);
    [self.view addSubview:scroll];
    UIImage* image = [UIImage imageNamed:@"5882b2b7d0a20cf4e3b656de74094b36acaf9989.jpg"];
    UIImageView* testImage = [[UIImageView alloc]initWithImage:image];
     testImage.image=image;
    [testImage setTransform:CGAffineTransformMakeScale(1, -1)];

    self.view.transform=CGAffineTransformMakeScale(1, -1);
    [testImage setFrame:CGRectMake(0, 0, testImage.image.size.width,
                                   testImage.image.size.height)];
    [scroll addSubview:testImage];
    
    CIImage* ciimage = [CIImage imageWithCGImage:image.CGImage];
    NSDictionary* opts = [NSDictionary dictionaryWithObject:
                          CIDetectorAccuracyHigh forKey:CIDetectorAccuracy];
    CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
                                              context:nil options:opts];
    NSArray* features = [detector featuresInImage:ciimage];
    
    for (CIFaceFeature *faceFeature in features){
        
        CGFloat faceWidth = faceFeature.bounds.size.width;
        
        // create a UIView using the bounds of the face
        UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
      
        NSLog(@"%@",NSStringFromCGRect(faceFeature.bounds));
        // add a border around the newly created UIView
        
        faceView.layer.borderWidth = 1;
        faceView.layer.borderColor = [[UIColor redColor] CGColor];
        
        [scroll addSubview:faceView];
        
        if(faceFeature.hasLeftEyePosition)
            
        {
            // create a UIView with a size based on the width of the face
            
            UIView* leftEyeView = [[UIView alloc] initWithFrame:
                                   CGRectMake(faceFeature.leftEyePosition.x-faceWidth*0.15,
                                              faceFeature.leftEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
            
            // change the background color of the eye view
            [leftEyeView setBackgroundColor:[[UIColor blueColor]
                                             colorWithAlphaComponent:0.3]];
            
            // set the position of the leftEyeView based on the face
            [leftEyeView setCenter:faceFeature.leftEyePosition];
            
            // round the corners
            leftEyeView.layer.cornerRadius = faceWidth*0.15;
            
            // add the view to the window
            [scroll  addSubview:leftEyeView];
            
        }
        
        if(faceFeature.hasRightEyePosition)
            
        {
            // create a UIView with a size based on the width of the face
            UIView* leftEye = [[UIView alloc] initWithFrame:
                               CGRectMake(faceFeature.rightEyePosition.x-faceWidth*0.15,
                                          faceFeature.rightEyePosition.y-faceWidth*0.15, faceWidth*0.3, faceWidth*0.3)];
            
            // change the background color of the eye view
            [leftEye setBackgroundColor:[[UIColor blueColor]
                                         colorWithAlphaComponent:0.3]];
            
            // set the position of the rightEyeView based on the face
            [leftEye setCenter:faceFeature.rightEyePosition];
            
            // round the corners
            leftEye.layer.cornerRadius = faceWidth*0.15;
            
            // add the new view to the window
            [scroll  addSubview:leftEye];
        }
        
        if(faceFeature.hasMouthPosition)
        {
            
            // create a UIView with a size based on the width of the face
            UIView* mouth = [[UIView alloc] initWithFrame:
                             CGRectMake(faceFeature.mouthPosition.x-faceWidth*0.2,
                                        faceFeature.mouthPosition.y-faceWidth*0.2, faceWidth*0.4, faceWidth*0.4)];
            
            // change the background color for the mouth to green
            [mouth setBackgroundColor:[[UIColor greenColor]
                                       colorWithAlphaComponent:0.3]];
            
            // set the position of the mouthView based on the face
            [mouth setCenter:faceFeature.mouthPosition];
            
            // round the corners
            mouth.layer.cornerRadius = faceWidth*0.2;
            
            // add the new view to the window
            [scroll  addSubview:mouth];
        }         
    }

}
-(void)QRSelector
{
   //二维码识别，不知为何识别不出来
        UIImage*image=[UIImage imageNamed:@"屏幕快照 2015-08-25 下午6.30.20.png"];
        CIImage*Cimage=[CIImage imageWithCGImage:image.CGImage];
        CIContext*text=[CIContext contextWithOptions:nil];
        CIDetector*detector=[CIDetector detectorOfType:CIDetectorTypeQRCode context:text options:[NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]];
        NSArray*array=[detector featuresInImage:Cimage];
        for(NSInteger i=0;i<array.count;i++){
            CIQRCodeFeature *feature=array[i];
            CGFloat w=self.view.bounds.size.width/3.0;
            CGFloat h=w;
            CGFloat x=i%3*w;
            CGFloat y=i/3*h;
            UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, w, h)];
            [self.view addSubview:imageView];
            NSLog(@"%@",NSStringFromCGRect(feature.bounds));
            CGImageRef ref=CGImageCreateWithImageInRect(image.CGImage, feature.bounds);
            imageView.image=[UIImage imageWithCGImage:ref];
            
        }
    
    


}
-(UIImage*)imageFormImage:(UIImage*)image{
//CG的上下文坐标，，y轴向上
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextDrawImage(context, CGRectMake(0,0, image.size.width, image.size.height), image.CGImage);

    UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;







}

@end
